package com.example.sale.dao;

import com.example.sale.entity.SaleChance;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Mapper
@Repository
public interface SaleDAO {
    final static Logger logger = LoggerFactory.getLogger(SaleDAO.class);
    /* 在java中写xml可读性和维护性太差，
     *所以这里借用内部类来生成动态SQL。
     *增改删查有对应的 @InsertProvider、@UpdateProvider、@DeleteProvider 、@SelectProvider
     * 可参考 ：https://www.cnblogs.com/guoyafenghome/p/9123442.html
     */

    /*
    @Select("<script> select * from data_dic " +
            "<if test=\"data_desc !=null \"> " +
            "where data_desc = #{data_desc} </if>" +
            " </script>")
    */
    @SelectProvider(type = SaleDAOProvider.class, method = "queryData")
    List<SaleChance> queryData(SaleChance saleChance);

    /*查询data_dic全部数据，后期使用 pagehelper 做分页处理*/
    @Select("select * from sale_chance ")
    List<SaleChance> queryDataAll();

    @Select("select * from sale_chance  where id = #{id}")
     SaleChance queryChanceById(String id);

    @Delete("delete from sale_chance where id = #{id}")
    void deleteInfo(String id);

    /* 内部类实现 update*/
    @UpdateProvider(type = SaleDAOProvider.class, method = "updateInfo")
     void updateInfo(SaleChance saleChance);

    @InsertProvider(type = SaleDAOProvider.class, method = "saveInfo")
    //@Options(useGeneratedKeys = true, keyProperty = "saleChance.id",keyColumn = "saleChance.id")
    void saveInfo(SaleChance saleChance, HttpServletRequest request);


    //内部类实现动态sql
    class SaleDAOProvider {
        public String queryData(SaleChance saleChance){
            //结构化sql
           /* FROM和WHERE：跟SELECT一样，可以写多个参数，也可以在多行重复使用，最终会智能合并而不会报错
            这样语句适用于写很长的SQL时，能够保证SQL结构清楚。便于维护，可读性高。*/
            return new SQL(){
                {
                SELECT("id,customer_name,summarize,linkman");
                SELECT("link_tel,create_time,creator,opportunity_desc");
                SELECT("opportunity_source,success_probability");  //会智能合并
                SELECT("designator,assignment_time,status");
                    FROM("sale_chance");
                    if(saleChance.getId()!=null && !(saleChance.getId().equals("")) ){
                        WHERE("id = #{id}");
                    }
                    if(!StringUtils.isEmpty(saleChance.getCustomer_name()) ){
                        WHERE("customer_name like '%"+saleChance.getCustomer_name()+"%' ");
                    }
                    if (saleChance.getSummarize()!=null && !(saleChance.getSummarize().equals("")) ){
                        WHERE("summarize like '%"+saleChance.getSummarize()+"%' ");
                    }
                    if (saleChance.getLinkman()!=null && !(saleChance.getLinkman().equals("")) ){
                        WHERE("linkman like '%"+saleChance.getLinkman()+"%' ");
                    }
                }
            }.toString();
        }

        public String updateInfo(SaleChance saleChance){
            return new SQL(){
                {
                    UPDATE("sale_chance");
                    if (!StringUtils.isEmpty(saleChance.getCustomer_name())) {
                        SET("customer_name = #{customer_name,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(saleChance.getSummarize())) {
                        SET("summarize= #{summarize,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!ObjectUtils.isEmpty(saleChance.getLinkman())) {
                        SET("linkman= #{linkman,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(saleChance.getLink_tel())) {
                        SET("link_tel= #{link_tel,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(saleChance.getOpportunity_desc())) {
                        SET("opportunity_desc= #{opportunity_desc,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(saleChance.getOpportunity_source())) {
                        SET("opportunity_source= #{opportunity_source,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(saleChance.getSuccess_probability())) {
                        SET("success_probability= #{success_probability,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(saleChance.getDesignator())) {
                        SET("designator= #{designator,javaType=String,jdbcType=VARCHAR}");
                    }
                    if (!StringUtils.isEmpty(saleChance.getAssignment_time())) {
                        SET("assignment_time= now()");
                    }else{
                        SET("assignment_time= now()");
                    }

                    if (!StringUtils.isEmpty(saleChance.getStatus())) {
                        SET("status= #{status,javaType=String,jdbcType=VARCHAR}");
                    }
                    WHERE("id = #{id,javaType=String,jdbcType=VARCHAR}" );
                }
            }.toString();

        }

        public String saveInfo(SaleChance saleChance, HttpServletRequest request){
           String sql =  new SQL(){
               {
                   INSERT_INTO("sale_chance");

                   if (!StringUtils.isEmpty(saleChance.getCustomer_name())) {
                       VALUES("customer_name"," '"+saleChance.getCustomer_name()+"'");
                   }
                   if (!StringUtils.isEmpty(saleChance.getSummarize())) {
                       VALUES("summarize"," '"+saleChance.getSummarize()+"'");
                   }
                   if (!ObjectUtils.isEmpty(saleChance.getLinkman())) {
                       VALUES("linkman"," '"+saleChance.getLinkman()+"'");
                   }
                   if (!StringUtils.isEmpty(saleChance.getLink_tel())) {
                       VALUES("link_tel"," '"+saleChance.getLink_tel()+"'");
                   }
                   if (!StringUtils.isEmpty(saleChance.getCreate_time())) {
                       VALUES("create_time"," '"+saleChance.getCreate_time()+"'");
                   }else{
                       VALUES("create_time","now()");
                   }
                   if (!StringUtils.isEmpty(saleChance.getCreator())) {
                       VALUES("creator"," '"+saleChance.getCreator()+"'");
                   }else{
                       VALUES("creator"," 'admin' ");
                   }
                   if (!StringUtils.isEmpty(saleChance.getOpportunity_desc())) {
                       VALUES("opportunity_desc"," '"+saleChance.getOpportunity_desc()+"'");
                   }
                   if (!StringUtils.isEmpty(saleChance.getOpportunity_source())) {
                       VALUES("opportunity_source"," '"+saleChance.getOpportunity_source()+"'");
                   }
                   if (!StringUtils.isEmpty(saleChance.getSuccess_probability())) {
                       VALUES("success_probability"," '"+saleChance.getSuccess_probability()+"'");
                   }
                   if (!StringUtils.isEmpty(saleChance.getDesignator())) {
                       VALUES("designator"," '"+saleChance.getDesignator()+"'");
                   }
                   if (!StringUtils.isEmpty(saleChance.getAssignment_time())) {
                       VALUES("assignment_time"," '"+saleChance.getAssignment_time()+"'");
                   }else{
                       VALUES("assignment_time","now()");
                   }
                   if (!StringUtils.isEmpty(saleChance.getStatus())) {
                       VALUES("status"," '"+saleChance.getStatus()+"'");
                   }
               }
           }.toString();
           logger.info("saveInfo:-->",sql);
            return sql;
        }
    }

}
